package com.cx.android;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;
import android.graphics.Bitmap; 

public class Code1009AppWidgetProvider extends AppWidgetProvider {
    public static String CLICK_ACTION   = "com.cx.android.appwidget.CLICK";
    public static String REFRESH_ACTION = "com.cx.android.appwidget.REFRESH";
    
	public Code1009AppWidgetProvider() {
    }

    @Override
    public void onEnabled(Context context) {
    	/*
    	final ContentResolver  cr  = context.getContentResolver();
    	final AppWidgetManager awm = AppWidgetManager.getInstance(context);
    	final ComponentName    cn  = new ComponentName(context, Code1009AppWidgetProvider.class);
    	*/
    }  
    
    @Override
    public void onReceive(Context ctx, Intent intent) {
        final String action = intent.getAction();
        
        if (action.equals(REFRESH_ACTION)) {
        	Log.i ("Code1009AppWidgetProvider", "onReceive() REFRESH_ACTION ");
        }
        else if (action.equals(CLICK_ACTION)) {
        	Log.i ("Code1009AppWidgetProvider", "onReceive() CLICK_ACTION ");
        }
        
        super.onReceive(ctx, intent);
    }
    
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    	final int N = appWidgetIds.length;
    	
    	 for (int i = 0; i < N; ++i) {             
    		 int appWidgetId = appWidgetIds[i];
    		 
    		 Intent intent = new Intent(context, Code1009Activity.class);
    		 PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
    		 
    		 RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.lo_appwidget);
    		 views.setOnClickPendingIntent(R.id.imageView1, pendingIntent);
    		 
    		 
    		 Code1009ImageFactory cif = new Code1009ImageFactory(context); 
    		 Bitmap bitmap = cif.getBitmap ();
    		 
    		 views.setImageViewBitmap (R.id.imageView1, bitmap);
    		 
             appWidgetManager.updateAppWidget(appWidgetId, views);
         }
    	 
    	//super.onUpdate(context, appWidgetManager, appWidgetIds);
    }
    
    /*
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    	
    	 for (int i = 0; i < appWidgetIds.length; ++i) {
             
    		 final Intent intent = new Intent(context, Code1009AppWidgetService.class);
             intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
             intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
             
             final RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.lo_appwidget);
             rv.setRemoteAdapter(appWidgetIds[i], R.id.imageView1, intent);

             // rv.setEmptyView(R.id.weather_list, R.id.empty_view);

             / *
             final Intent onClickIntent = new Intent(context, Code1009AppWidgetProvider.class);
             onClickIntent.setAction(Code1009AppWidgetProvider.CLICK_ACTION);
             onClickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
             onClickIntent.setData(Uri.parse(onClickIntent.toUri(Intent.URI_INTENT_SCHEME)));
             
             final PendingIntent onClickPendingIntent = PendingIntent.getBroadcast(context, 0, onClickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
             rv.setPendingIntentTemplate(R.id.weather_list, onClickPendingIntent);

             final Intent refreshIntent = new Intent(context, Code1009AppWidgetProvider.class);
             refreshIntent.setAction(Code1009AppWidgetProvider.REFRESH_ACTION);
             final PendingIntent refreshPendingIntent = PendingIntent.getBroadcast(context, 0, refreshIntent, PendingIntent.FLAG_UPDATE_CURRENT);
             rv.setOnClickPendingIntent(R.id.refresh, refreshPendingIntent);
             * /
             
             appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
         }
    	 
    	super.onUpdate(context, appWidgetManager, appWidgetIds);
    }
    */
}
